package com.chouren.goandroid.ui.listview.indexable;

import android.content.Context;
import android.database.Cursor;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AlphabetIndexer;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.chouren.goandroid.R;

public class IndexableListView extends LinearLayout implements OnScrollListener, LetterView.OnLetterChangeListener {
	private ListView mListView;
	private IndexableListViewAdapter mListAdapter;
	private AlphabetIndexer indexer;
	private String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	private LetterView letterView;
	private Toast toast;
	private TextView mToastText;
	private View mOverlay;
	private TextView mTextOverlay;
	private Cursor cursor;
	private String clumnName;

	public IndexableListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		initView();
		initToast();
	}
	
	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		if(indexer != null) {
			int section = indexer.getSectionForPosition(firstVisibleItem);
			letterView.setSelectedIndex(section);
			
			//设置挤压效果  
			int nextSection = section + 1;
			int pos = indexer.getPositionForSection(nextSection);
			//当当前第一个可见的item的下一个 正好是当前第一个可见item所在分组的下一个分组的第一个元素的时候
			if(pos == firstVisibleItem+1){
				//获取ListView的第一个孩子视图
				View v = view.getChildAt(0);
				if(v == null){
					return;
				}
				int dex = v.getBottom() - mOverlay.getHeight();
				if(dex <= 0 ){
					mOverlay.setPadding(0, dex, 0, 0);
					mTextOverlay.setText(alphabet.charAt(section)+"");
				}else{
					mOverlay.setPadding(0, 0, 0, 0);
					mTextOverlay.setText(alphabet.charAt(section)+"");
				}
			}else{
				mOverlay.setPadding(0, 0, 0, 0);
				mTextOverlay.setText(alphabet.charAt(section)+"");
			}
		}
	}

	@Override
	public void onLetterChange(int selectedIndex) {
		int position = indexer.getPositionForSection(selectedIndex);
		mListView.setSelection(position);
		mToastText.setText(String.valueOf(alphabet.charAt(selectedIndex)));
		toast.show();
	}

	private void initView() {
		View view = LayoutInflater.from(getContext()).inflate(R.layout.indexable_list_view, 
				this, true);
		mListView = (ListView) view.findViewById(R.id.listview);
		mOverlay = (View)view.findViewById(R.id.view_overlay);
		mTextOverlay = (TextView)view.findViewById(R.id.text_overlay);
		letterView = (LetterView)view.findViewById(R.id.lettersIndex);
		
		mListView.setOnScrollListener(this);
		letterView.setOnLetterChangeListener(this);
	}

	private void initToast() {
		View toastView = LayoutInflater.from(getContext()).inflate(
				R.layout.toast, new LinearLayout(getContext()), false);
		mToastText = (TextView) toastView.findViewById(R.id.tvToast);
		toast = new Toast(getContext());
		toast.setDuration(Toast.LENGTH_SHORT);
		toast.setView(toastView);
		toast.setGravity(Gravity.CENTER, 0, 0);
	}
	
	public void setmListAdapter(IndexableListViewAdapter mListAdapter) {
		this.mListAdapter = mListAdapter;
	}

	public void setCursor(Cursor cursor) {
		this.cursor = cursor;
	}

	public void setClumnName(String clumnName) {
		this.clumnName = clumnName;
	}
	
	public void dataSetHasChanged() {
		indexer = new AlphabetIndexer(cursor, cursor.getColumnIndex(clumnName), alphabet);
		mListAdapter = new IndexableListViewAdapter(cursor, getContext(), indexer, clumnName);
		
		mListView.setAdapter(mListAdapter);
		mListAdapter.notifyDataSetChanged();
	}

}
